<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/21
 * Time: 下午5:09
 */
class Withdraw_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }

    //添加提现记录 需要提现方式type，姓名name,提现账号account,提现金额money
    public function apply_for_tixian($param)
    {

        $where = array('shop_id' => $param['shop_id'], 'status' => 0);
        $item1 = $this->db->where($where)->from('app_withdraw')->get()->row_array();
        if ($item1) {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "您已申请提现，请等待后台审核"));
        }

        $where = array('shop_id' => $param['shop_id']);
        $item2 = $this->db->where($where)->from('app_property')->get()->row_array();
        if ($param['money'] > $item2['normal']) {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "可提现金额额度不足"));
        }

        $withdrawMaxId = $this->db->query("select MAX(id) as maxid from app_withdraw")->row_array()['maxid'];
        $withdrawMaxId = $withdrawMaxId + 1;
        $withdrawBn = 'W'.sprintf("%06d", $withdrawMaxId);
        $data               = array();
        $data['shop_id'] = $param['shop_id'];
        $data['withdraw_bn'] = $withdrawBn;
        $data['type']    = $param['type'];
        $data['money']      = $param['money'];
        $data['name']        = $param['name'];
        $data['account']        = $param['account'];
        $data['bank']        = isset($param['bank'])?$param['bank']:'';
        $data['status']     = 0;
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $res                = $this->db->insert('app_withdraw', $data);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    //获取全部可提现金额
    public function get_all_withdrawal($user_id)
    {
        $where = array('shop_id' => $user_id);
        $item  = $this->db->select('normal')->where($where)->from('app_property')->get()->row_array();
        if ($item) {
            return $item['normal'];
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "获取失败"));
        }
    }

    //获取提现日志
    public function get_withdrawal_list($shop_id)
    {
        $item = $this->db->query("select * from app_withdraw where shop_id = '$shop_id' order by id desc")->result_array();
        if ($item) {
            return $item;
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "获取失败"));
        }
    }
}
